package cn.stack.springframework.test;

import cn.bugstack.springframework.beans.PropertyValue;
import cn.bugstack.springframework.beans.PropertyValues;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.BeanReference;
import cn.bugstack.springframework.beans.factory.support.DefaultListableBeanFactory;
import cn.stack.springframework.test.bean.UserDao;
import cn.stack.springframework.test.bean.UserService;
import org.junit.Test;

public class ApiTest04 {
    @Test
    public void test_beanFactory(){
        //初始化BeanFactory
        DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();

        //UserDao注册
        beanFactory.registerBeanDefinition("userDao",new BeanDefinition(UserDao.class));

        //UserService 设置属性
        PropertyValues propertyValues=new PropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("uId","10003"));
        propertyValues.addPropertyValue(new PropertyValue("userDao",new BeanReference("userDao")));

        //UserService 注入bean
        BeanDefinition beanDefinition=new BeanDefinition(UserService.class,propertyValues);
        beanFactory.registerBeanDefinition("userService",beanDefinition);

        //UserService获取bean
        UserService userService= (UserService) beanFactory.getBean("userService");
        userService.queryUserInfo();
    }
}
